package org.droidplanner.services.android.impl.communication.connection;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.skydroid.rcsdk.PipelineManager;
import com.skydroid.rcsdk.comm.CommListener;
import com.skydroid.rcsdk.common.error.SkyException;
import com.skydroid.rcsdk.common.pipeline.Pipeline;

import org.droidplanner.services.android.impl.core.MAVLink.connection.MavLinkConnectionTypes;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * H12、S1 串口连接
 */
public class UartConnection extends AndroidMavLinkConnection {
    private static final String UART_TAG = UartConnection.class.getSimpleName();
    protected final int mBaudRate;
    protected final String mDevicePath;

    private Pipeline mSerialPort;
    private LinkedBlockingQueue<byte[]> uartBuff = new LinkedBlockingQueue<byte[]>();
    /**
     * 1.初始化数据 波特率
     */
    public UartConnection(Context parentContext, String devicePath, int baudRate) {
        super(parentContext);
        mBaudRate = baudRate;
        mDevicePath = devicePath;
    }

    /**
     * 3.建立连接(点击连接)
     */
    @Override
    protected void openConnection(Bundle connectionExtras) throws IOException {
        if (mSerialPort == null) {
            try {
                mSerialPort = PipelineManager.INSTANCE.createSerialPipeline(mDevicePath,mBaudRate);
                mSerialPort.setOnCommListener(new CommListener() {
                    @Override
                    public void onConnectSuccess() {

                    }

                    @Override
                    public void onConnectFail(SkyException e) {

                    }

                    @Override
                    public void onDisconnect() {

                    }

                    @Override
                    public void onReadData(byte[] bytes) {
                        try {
                            uartBuff.put(bytes);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                PipelineManager.INSTANCE.connectPipeline(mSerialPort);
            } catch (SecurityException e) {
                e.printStackTrace();
                return;
            }

        }
        onConnectionOpened(connectionExtras);
    }

    @Override
    protected int readDataBlock(byte[] buffer) throws IOException {
        return 0;
    }

    /**
     * 4.读取数据(不断发送和不断读取，不分先后)
     */
    @Override
    protected byte[] readDataBlock2() {
        try {
            return uartBuff.take();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected boolean isUseReadData2() {
        return true;
    }

    /**
     * 5.发送数据(不断发送和不断读取，不分先后)
     */
    @Override
    protected void sendBuffer(byte[] buffer) throws IOException {
        // printLogger(UART_TAG, "## sendBuffer 5##");
        try {
            mSerialPort.writeData(buffer);
        }catch (Exception e){}
    }

    /**
     * 6.关闭串口(点击断开连接)
     */
    @Override
    protected void closeConnection() throws IOException {
        if (mSerialPort != null) {
            PipelineManager.INSTANCE.disconnectPipeline(mSerialPort);
            mSerialPort = null;
        }

    }

    /**
     * 2.加载参数(没用到)
     */
    @Override
    protected void loadPreferences() {

    }

    /**
     * mavlink 连接的类型 (没用)
     *
     * @return The type of this mavlink connection.
     */
    @Override
    public int getConnectionType() {
        return MavLinkConnectionTypes.MAVLINK_CONNECTION_H12;
    }
}
